#include "connect.hpp"

using namespace std;

int main()
{
    //创建管道
    int n = mkfifo(FIFO_FILE,MODE);
    if(n == -1)
    {
        perror("creat fail");
        exit(FIFO_CREAT_ERR);
    }
    //打开管道
    int fd = open(FIFO_FILE,O_RDONLY);
    if(fd < 0)
    {
        perror("open fail");
        exit(FIFO_OPEN_ERR);
    }
    //进行通信
    while(1)
    {
        char buffer[NUM] = {0};
        int x = read(fd,buffer,sizeof(buffer));
        if(x > 0)
        {
            buffer[x] = 0;
            cout<<"client send to server@"<<buffer<<endl;
        }
        else if(x == 0)
        {
            cout<<"client quit, me too!"<<endl;
            break;
        }
        else break;

    }
    //关闭管道
    close(fd);
    //删除管道
    int m = unlink(FIFO_FILE);
    if(m == -1)
    {
        perror("delete fail");
        exit(FIFO_DELETE_ERR);
    }
    return 0;
}